package com.xinrui.sunnyday.security;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xinrui.sunnyday.pojo.Permission;
import com.xinrui.sunnyday.pojo.Role;
import com.xinrui.sunnyday.pojo.User;
import com.xinrui.sunnyday.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class UserInfoService implements UserDetailsService {
    @Reference
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取用户名
        User user = userService.findByUsername(username);
        //判断此用户存在
        if (null!=user){
            //获取用户密码
            String password = user.getPassword();
            //建立一个权限和角色公用的集合
            List<GrantedAuthority> authorityList = new ArrayList<>();
            GrantedAuthority authority = null;
            //获取用户拥有的角色
            Set<Role> roles = user.getRoles();
            //判断此角色存在
            if (null!=roles) {
                for (Role role : roles) {
                    //添加一个角色关键字
                    authority = new SimpleGrantedAuthority(role.getKeyword());
                    authorityList.add(authority);
                    //建立一个权限集合
                    //因为可能需要添加多个权限且不能重复
                    Set<Permission> permissionSet = role.getPermissions();
                    //判断该集合是否存在
                    if (null != permissionSet) {
                        //遍历集合 拿到实体类元素
                        for (Permission permission : permissionSet) {
                            //添加权限
                            authority = new SimpleGrantedAuthority(permission.getKeyword());
                            authorityList.add(authority);
                        }
                    }
                }
            }
            org.springframework.security.core.userdetails.User securityUser = new org.springframework.security.core.userdetails.User(username,password,authorityList);
            return securityUser;
        }
        return null;
    }

    //写死密码进行初始测试
//    public User findByUsername(String username){
//        if ("JNC".equals(username)){
//            User user = new User();
//            user.setUsername("JNC");
//            user.setPassword("$2a$10$lM48fnSkc/3pfgbAGJLBpOMwTPpQM9jcKo0mShcLHWHwqW7hSHvxG");
//            return user;
//        }
//        return null;
//    }

//    public static void main(String[] args) {
//        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
//        System.out.println(bc.encode("1234"));
////        System.out.println(bc.matches("123456","$2a$10$dX1B45ZBCqyZFIMsbajzYe6vTYiYQ8HlXgHmk/fwfNIj/c9OryBX."));
//    }
}
